import Vue from 'vue'
import Cookies from 'js-cookie'
import Element from 'element-ui'
import './assets/styles/element-variables.scss'
import '@/assets/styles/index.scss' // global css
import '@/assets/styles/ruoyi.scss' // ruoyi css
import App from './App'
import store from './store'
import router from './router'
import directive from './directive' // directive
import plugins from './plugins' // plugins
import { download } from '@/utils/request'
import './assets/icons' // icon
import './permission' // permission control
import { getDicts } from "@/api/system/dict/data";
import { getConfigKey } from "@/api/system/config";
import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, handleTree } from "@/utils/ruoyi";
import LangSelect from "@/components/LangSelect"
// 分页组件
import Pagination from "@/components/Pagination";
// 自定义表格工具组件
import RightToolbar from "@/components/RightToolbar"
// 富文本组件
import Editor from "@/components/Editor"
// 文件上传组件
import FileUploadd from "@/components/FileUploadd"
import FileUpload from "@/components/FileUpload"
import AudiooUpload from "@/components/AudiooUpload"
import AudioUpload from "@/components/AudioUpload"
import ImgUpload from "@/components/ImgUpload"
// 图片上传组件
import ImageUpload from "@/components/ImageUpload"
// 字典标签组件
import DictTag from '@/components/DictTag'
// 头部标签组件
import VueMeta from 'vue-meta'
// 字典数据组件
import DictData from '@/components/DictData'
// Echart
import echarts from 'echarts'
// 一键复制粘贴板组件
import VueClipboard from 'vue-clipboard2'
// Mqtt工具
import mqttTool from '@/utils/mqttTool'
import ItemWrap from './views/bigScreen/components/item-wrap/item-wrap.vue'
import Message from './views/bigScreen/components/message/message.vue'
import Reacquire from './views/bigScreen/components/reacquire/reacquire.vue'
import Echart from './views/bigScreen/components/echart/index.vue'
import {loading,borderBox13,digitalFlop,capsuleChart,borderBox8} from '@jiaminghi/data-view'
import * as filters from '@/directive/filters'
import i18n from './lang' // 引入i18n实例
import 'element-ui/lib/theme-chalk/index.css'
require('./mock/mock')//是否使用mock

// 获取语言设置函数
function getLanguage() {
  const chooseLanguage = Cookies.get('language')
  if (chooseLanguage) return chooseLanguage

  // 如果没有获取到语言，使用浏览器语言或默认中文
  const language = (navigator.language || navigator.browserLanguage).toLowerCase()
  const locales = Object.keys(i18n.messages)
  for (const locale of locales) {
    if (language.indexOf(locale) > -1) {
      return locale
    }
  }
  return 'zh-CN'
}

// 加载 Element UI 语言包
async function loadElementUILocale(lang) {
  try {
    switch (lang) {
      case 'en':
        return (await import('element-ui/lib/locale/lang/en')).default
      case 'th':
        return (await import('element-ui/lib/locale/lang/th')).default
      default:
        return (await import('element-ui/lib/locale/lang/zh-CN')).default
    }
  } catch (error) {
    console.warn('加载 Element UI 语言包失败:', error)
    return (await import('element-ui/lib/locale/lang/zh-CN')).default
  }
}

// 初始化 Element UI 配置
async function initializeElementUI() {
  const language = getLanguage()
  const elementUILocale = await loadElementUILocale(language)

  // 修复 Element UI 配置，确保 i18n 正确传递
  Vue.use(Element, {
    size: Cookies.get('size') || 'medium',
    i18n: (key, value) => i18n.t(key, value),
    locale: elementUILocale
  })
}

// 初始化应用
async function initializeApp() {
  // 等待 Element UI 初始化完成
  await initializeElementUI()

  // datav组件
  Vue.use(loading)
  Vue.use(borderBox13)
  Vue.use(borderBox8)
  Vue.use(digitalFlop)
  Vue.use(capsuleChart)
  // 自定义组件
  Vue.component("Echart",Echart)
  Vue.component("ItemWrap",ItemWrap)
  Vue.component("Message",Message)
  Vue.component("Reacquire",Reacquire)
  // 全局方法挂载
  Vue.prototype.getDicts = getDicts
  Vue.prototype.getConfigKey = getConfigKey
  Vue.prototype.parseTime = parseTime
  Vue.prototype.resetForm = resetForm
  Vue.prototype.addDateRange = addDateRange
  Vue.prototype.selectDictLabel = selectDictLabel
  Vue.prototype.selectDictLabels = selectDictLabels
  Vue.prototype.download = download
  Vue.prototype.handleTree = handleTree
  Vue.prototype.$echarts = echarts
  Vue.prototype.$mqttTool = mqttTool
  // 全局组件挂载
  Vue.component('DictTag', DictTag)
  Vue.component('Pagination', Pagination)
  Vue.component('RightToolbar', RightToolbar)
  Vue.component('Editor', Editor)
  Vue.component('LangSelect', LangSelect)
  Vue.component('FileUploadd', FileUploadd)
  Vue.component('FileUpload', FileUpload)
  Vue.component('AudiooUpload', AudiooUpload)
  Vue.component('AudioUpload', AudioUpload)
  Vue.component('ImageUpload', ImageUpload)
  Vue.component('ImgUpload', ImgUpload)
  Vue.use(VueClipboard)
  Vue.use(directive)
  Vue.use(plugins)
  Vue.use(VueMeta)
  DictData.install()

  // 全局数据过滤器
  Object.keys(filters).forEach(k => Vue.filter(k, filters[k]));

  Vue.config.productionTip = false

  new Vue({
    el: '#app',
    router,
    store,
    i18n, // 挂载i18n实例
    render: h => h(App)
  })

  // 带有边框
  Element.Table.props.border = {
    default:true,
    type:Boolean
  }
}

// 启动应用
initializeApp().catch(error => {
  console.error('应用初始化失败:', error)
})
